package com.beiding.workordersystem.orders.product.controller;

import com.beiding.workordersystem.common.dao.MPage;
import com.beiding.workordersystem.common.standard.R;
import com.beiding.workordersystem.common.utils.MyBeanUtils;
import com.beiding.workordersystem.orders.product.entity.Product;
import com.beiding.workordersystem.orders.product.entity.ProductType;
import com.beiding.workordersystem.orders.product.service.ProductService;
import com.beiding.workordersystem.orders.product.vo.ProductTypeVo;
import com.beiding.workordersystem.orders.product.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService service;

    //获取树状结构
    @GetMapping("/type/tree")
    R typeTree(){
        return R.ok(service.treeType());
    }

    @PostMapping("/type/save")
    R typeSave(@RequestBody @Validated ProductTypeVo vo) {
        ProductType productType = MyBeanUtils.copy(vo, ProductType.class);
        service.saveType(productType);
        return R.ok(productType.getId());
    }

    @PostMapping("/type/delete")
    R typeDelete(@RequestBody String[] ids) {
        service.deleteType(ids);
        return R.ok();
    }

    @PostMapping("/save")
    R save(@RequestBody @Validated ProductVo vo) {
        return R.ok(service.save(MyBeanUtils.copy(vo, Product.class)));
    }

    @GetMapping("/page")
    R page(MPage page,@RequestParam String typeId) {//分页参数
        return R.ok(service.page(page,typeId));
    }

    @GetMapping("/list")
    R list(){
        return R.ok(service.list());
    }

    @PostMapping("/delete")
    R delete(@RequestBody String[] ids) {
        service.delete(ids);
        return R.ok();
    }

}
